//
//  LCHorizontalGrideView.m
//  JKHorizontalGrideView
//
//  Created by jiukuyinyue on 2017/8/18.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LCHorizontalGrideView.h"
#import "LCHorizontalGrideCollectionViewLayout.h"
#import "Masonry/Masonry.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface LCHorizontalGrideViewCell : UICollectionViewCell

@property (strong, nonatomic)   UILabel         *titleLabel;

@property (strong, nonatomic)   UIImageView     *iconImageView;

@property (assign, nonatomic)   NSInteger       defaultBottom;

@property (assign, nonatomic)   NSInteger       celltype;


@end

@implementation LCHorizontalGrideViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _titleLabel = [UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_titleLabel];        
        _iconImageView = [UIImageView new];
        [self.contentView addSubview:_iconImageView];
        //剪裁子视图
        self.iconImageView.layer.cornerRadius = _iconImageView.bounds.size.width;
        self.iconImageView.clipsToBounds = YES;
 
    }
    return self;
}
-(void)setCelltype:(NSInteger)celltype{
    
    CGFloat width = [[UIScreen mainScreen] bounds].size.width / 6;
    
    if (celltype == 1) {//奉献支持样式
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.height.mas_equalTo(40);
            make.bottom.equalTo(self.contentView.mas_bottom).offset(0);
        }];
        [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(width, width));
            make.centerX.equalTo(self.contentView);
            make.bottom.equalTo(_titleLabel.mas_top);
            //                make.top.equalTo(self.contentView.mas_top);
        }];
    }else{
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
//            make.bottom.equalTo(self.contentView.mas_bottom).offset(0);
            make.height.mas_equalTo(40);
        }];
        [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(width, width));
            make.centerX.equalTo(self.contentView);
            make.bottom.equalTo(_titleLabel.mas_top);
            make.top.equalTo(self.contentView.mas_top).offset(13);
        }];
    }
}
@end

@interface LCHorizontalGrideView () <UICollectionViewDataSource,UICollectionViewDelegate>

@property (strong, nonatomic)   UICollectionView    *collectionView;

@property (strong, nonatomic)   LCHorizontalGrideCollectionViewLayout *layout;

@end

@implementation LCHorizontalGrideView

- (UICollectionView *)collectionView {
    if(_collectionView == nil) {
        self.layout = [LCHorizontalGrideCollectionViewLayout new];
        
        // 设置行数
        if(self.delegate && [self.delegate respondsToSelector:@selector(numberOfRowsPerPageInHorizontalGrideView:)]) {
            self.layout.rowCount = [self.delegate numberOfRowsPerPageInHorizontalGrideView:self];
        } else {
            self.layout.rowCount = 2;
        }
        
        // 设置列数
        if(self.delegate && [self.delegate respondsToSelector:@selector(numberOfColumnsPerPageInHorizontalGrideView:)]) {
            self.layout.columnCount = [self.delegate numberOfColumnsPerPageInHorizontalGrideView:self];
        } else {
            self.layout.columnCount = 4;
        }
        
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _collectionView.pagingEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerClass:[LCHorizontalGrideViewCell class] forCellWithReuseIdentifier:@"CELL"];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self);
            make.bottom.equalTo(self);//.pageControl.mas_top
         }];
    }
    return _collectionView;
}

/**
 刷新
 */
- (void)reloadData {
    [self.collectionView reloadData];
    
}

#pragma mark - UICollectionViewDataSource -
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = 0;
    if(self.dataSource && [self.dataSource respondsToSelector:@selector(numberOfItemsInHorizontalGrideView:)]) {
        count = [self.dataSource numberOfItemsInHorizontalGrideView:self];
    }
    return count;
}

- (__kindof LCHorizontalGrideViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCHorizontalGrideViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    cell.celltype = self.celltype;
    NSString *title = @"";
    if(self.dataSource && [self.dataSource respondsToSelector:@selector(horizontalGrideView:titleForItemAtIndex:)]) {
        title = [self.dataSource horizontalGrideView:self titleForItemAtIndex:indexPath.row];
    }
    cell.titleLabel.text = title;
    
    if(self.dataSource && [self.dataSource respondsToSelector:@selector(horizontalGrideView:iconURLForItemAtIndex:)]) {
        
        NSString *url = [self.dataSource horizontalGrideView:self iconURLForItemAtIndex:indexPath.row];
        
        if(self.defaultImage) {
            cell.iconImageView.image = self.defaultImage;
        } else {
            cell.iconImageView.image = [UIImage imageNamed:url];
        }
    }
    
    return cell;
}


#pragma mark - UICollectionViewDelegate -
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if(self.delegate && [self.delegate respondsToSelector:@selector(horizontalGrideView:didSelectItemAtIndex:)]) {
        [self.delegate horizontalGrideView:self didSelectItemAtIndex:indexPath.row];
    }
}
@end
